﻿using loginWinForm.common;
using loginWinForm.content;
using loginWinForm.entity;
using loginWinForm.service;
using loginWinForm.service.impl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace loginWinForm.form
{
    public partial class ChangePwd : Form
    {
        private ILoginService loginService = new LoginService();

        public ChangePwd()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            new Success().Show();
        }

        private void changeTj_Click(object sender, EventArgs e)
        {
            string pwd = CONTENT.SysUser.Password;
            string pwdOld = oldPwd.Text;
            string pwdNew = newPwd.Text;
            string pwdCheck = checkPwd.Text;
            if (pwdOld.Length <= 0)
            {
                ResultBox.error("原密码不能为空");
                return;
            }
            if (pwdNew.Length <= 0)
            {
                ResultBox.error("新密码不能为空");
                return;
            }
            if (pwdCheck.Length <= 0)
            {
                ResultBox.error("确认密码不能为空");
                return;
            }


            // 需要加入MD5编码
            if (!pwd.Equals(pwdOld))
            {
                ResultBox.error("原密码错误");
                oldPwd.Text = "";
                return;
            }
            if (!pwdNew.Equals(pwdCheck))
            {
                ResultBox.error("2次密码输入不一致");
                newPwd.Text = "";
                checkPwd.Text = "";
                return;
            }

            CONTENT.SysUser.Password = pwdNew;
            loginService.update(CONTENT.SysUser);
            ResultBox.ok("密码修改成功，请重新登录");
            CONTENT.SysUser = null;
            this.Hide();
            new login().Show();
        }
    }
}
